<?php
include_once('common/header.php');
if($AppUI->id <= 0 || (isset($_SESSION['share_manual']) && (($_SESSION['share_manual']&1<<2) == 0))) {
	$this->_redirect(HOST_BACKEND);
}
$intId			= $objRequest->getParam('pid', 0);
$strTitle		= $objRequest->getParam('ptitle', '');
$strContent		= $objRequest->getParam('pcontent', '');
$oFile			= $_FILES['pimage'];
$strFlash		= $objRequest->getParam('pflash', '');
$strLink		= $objRequest->getParam('plink', '');
$strShort		= $objRequest->getParam('pshort', '');
$intCategoryId	= (int)$objRequest->getParam('pcategoryid', 0);
$intSubId		= (int)$objRequest->getParam('psubid', 0);
$strTag			= $objRequest->getParam('ptag', '');
$arrErr			= null;
$strImage		= '';
//data
$strTitle		= Ecore_Function::stripTags($strTitle,-2);
$strShort		= Ecore_Function::stripTags($strShort,-2);
$strContent		= Ecore_Function::stripTags($strContent,-1);
$strTag			= Ecore_Function::stripTags($strTag,-2);
//validate
if($intCategoryId < 1) array_push($arrErr, $arrLocale['category_required']);
if($intSubId < 1) array_push($arrErr, $arrLocale['sub_required']);
if($strTitle == '') array_push($arrErr, $arrLocale['name_required']);
elseif($strTitle != '' && strlen($strTitle) < VL_NAME_MINLENGTH) array_push($arrErr, $arrLocale['name_minlength']);
if($strShort != '' && strlen($strShort) < VL_SHORT_MINLENGTH) array_push($arrErr, $arrLocale['short_minlength']);
elseif($strShort != '' && strlen($strShort) > VL_SHORT_MAXLENGTH) array_push($arrErr, $arrLocale['short_maxlength']);
if($strContent == '') array_push($arrErr, $arrLocale['content_required']);
elseif($strContent != '' && strlen($strContent) < VL_CONTENT_MINLENGTH) array_push($arrErr, $arrLocale['content_minlength']);
if($strLink != '' && !Ecore_Function::validateUrl($strLink)) array_push($arrErr, $arrLocale['link_format']);
//update
if(!isset($arrErr) && isset($oFile)) {
	$arrImage	= Ecore_Upload_Share::upload($oFile);
	if($arrImage['error'] == 0) {
		$strImage	= $arrImage['path'];
	}else {
		array_push($arrErr, $arrError['upload']);
	}
}
if(!isset($arrErr)) {
	if(isset($_SESSION['SHARE_MANUAL_POST'])) unset($_SESSION['SHARE_MANUAL_POST']);
	if(isset($_SESSION['SHARE_MANUAL_ERROR'])) unset($_SESSION['SHARE_MANUAL_ERROR']);
	$strShort	= $strShort == '' ? Ecore_Function::wordCut($strContent) : $strShort;  
	$objSmanual = new Smanual();
	$intError	= $objSmanual->update($AppUI->id, 
									  $intId, 
									  $strTitle, 
									  $strShort, 
									  $strContent, 
									  $strImage, 
									  $strFlash, 
									  $strLink, 
									  $intCategoryId, 
									  $intSubId, 
									  $strTag
									 );
	if($intError == 0) Ecore_Function::setMsg($arrMessage['smanual_edit']);
	else array_push($arrErr, $arrError['noperm_smanual_edit']);
}
if(isset($arrErr)) {
	$_SESSION['SHARE_MANUAL_POST']	= $objRequest->getParams();
	$_SESSION['SHARE_MANUAL_ERROR']	= $arrError[$intError];
	$this->_redirect(HOST_BACKEND . 'smanual/edit?id='.$intId);
}
$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND . 'ecourse/index');